package com.css.apps.base.user.action.login;

import java.util.HashMap;
import java.util.List;

import com.css.apps.base.common.BaseEnvironment;
import com.css.apps.base.sys.action.SysItem;
import com.css.apps.base.sys.model.SSys;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.spi.IUserListener;
import com.css.common.PluginBus;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.AbstractAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public abstract class LoginAction extends AbstractAction {
	public String result;
	public SUser sUser = new SUser();
	public String userId = null;
	public Integer autoLogin = null;
	public int mod = 0;
	public String funcid = "ACL_LOGIN"; // 功能编码

	@SuppressWarnings("deprecation")
	public String go() {
		String sRet = loginGo();
		if (sRet != Action.SUCCESS)
			return sRet;
		if (sUser == null)
			sUser = QueryCache.get(SUser.class, userId);
		/*
		 * if (sUser == null) {
		 * setMessage(Messages.getString("systemMsg.loginFaild")); result =
		 * Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
		 * Messages.getString("systemMsg.loginFaild")); return Action.ERROR; }
		 * 
		 * if ("2".equals(sUser.getOpenFlag())) {
		 * setMessage(Messages.getString("systemMsg.accountCloseError")); result
		 * = Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
		 * Messages.getString("systemMsg.accountCloseError")); return
		 * Action.ERROR; } String password =
		 * Md5Util.MD5Encode(Environment.DEFAULT_PASSWORD); if
		 * (password.equalsIgnoreCase(sUser.getPassword())) { result =
		 * Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
		 * Messages.getString("systemMsg.pwdError"));
		 * setMessage(Messages.getString("systemMsg.pwdError")); return
		 * Action.ERROR; }
		 */
		// 登录成功后，总登录次数加1，失败次数清零
		sUser.updateTotalLoginCount();
		String systemId = com.css.core.configuration.ConfigurationManager.getConfigurationManager()
				.getSysConfigure("app.system.id", Environment.CURRENT_SYSTEM_ID);
		if ("2".equals(QueryCache.get(SSys.class, systemId).getOpenFlag())) {
			if (sUser.getUserType().equals(BaseEnvironment.USERTYPE_NORMAL)) {
				String msg = Messages.getString("systemMsg.systemUnavailable");
				setMessage(msg);
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, msg);
				return Action.ERROR;
			}
		}

		IUserListener lis = PluginBus.getPlugin(IUserListener.class);
		if (lis != null) {
			lis.afterLogin(sUser);
		}
		List<SSys> sysList = SysItem.getSystems();
		HashMap<String, SSys> sysmap = new HashMap<>();
		for (SSys sys : sysList) {
			sysmap.put(sys.getSysId(), sys);
		}
		set("sysMap", sysmap);
		// 将sUser设置在session中
		set(Environment.SESSION_LOGIN_KEY, sUser);
		// 一个userId绑定一个ip
		// set(sUser.getUuid(),
		// ServletActionContext.getRequest().getRemoteHost());
		/*
		 * if (mod == 2) { if (autoLogin != null && autoLogin.intValue() == 1)
		 * CookieUtil.SetCookies(Environment.Cookie_UserID,
		 * DesUtil.encrypt(sUser.getUuid().toString()), 24 * 60 * 60 * 30,
		 * ServletActionContext.getResponse()); else UserItem.clearCookies(); }
		 */
		result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, String.valueOf(sUser.getUserType()));
		setMessage(Messages.getString("systemMsg.success"));
		return Action.SUCCESS;
	}

	protected abstract String loginGo();

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public SUser getSUser() {
		return sUser;
	}

	public void setSUser(SUser user) {
		sUser = user;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Integer getAutoLogin() {
		return autoLogin;
	}

	public void setAutoLogin(Integer autoLogin) {
		this.autoLogin = autoLogin;
	}

	public int getMod() {
		return mod;
	}

	public void setMod(int mod) {
		this.mod = mod;
	}
}
